$(function () {
    cameraTree();
});



function playback(){//回放

     var nowDate = new Date();
     var year = nowDate.getFullYear();
     var month = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1)
         : nowDate.getMonth() + 1;
     var day = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate
         .getDate();
     var dateStr = year + "-" + month + "-" + day;
     $("#huifang").removeClass("layui-btn-normal");
     $("#shishi").addClass("layui-btn-normal");
     $("#beginTime").val(dateStr+" 00:00:00");
     $("#endTime").val(dateStr+" 23:59:59");
     $("#camaraList").css("height","60%");
     $("#timediv").css("display","block")

    window.parent.uninit();
    window.parent.layout = "1x1";
    window.parent.playMode=1;
    window.parent.init();
 }
 function realTime(){//实时
     $("#shishi").removeClass("layui-btn-normal");
     $("#huifang").addClass("layui-btn-normal");
     $("#camaraList").css("height","100%");
     $("#timediv").css("display","none");

     window.parent.uninit();
     window.parent.layout = "2x2";
     window.parent.playMode=0;
     window.parent.init();
 }

//视频预览功能
function startPreview(cameraIndexCode){
    //$(".camera-run").attr("src","../statics/css/img/.png");
    var name =$(".camera-run").attr("name");
    if(name=="camera-left"){
        $(".camera-run").attr("src","../../statics/css/img/camera-left.png");
        $(".camera-run").removeClass("camera-run");
    }else {
        $(".camera-run").attr("src","../../statics/css/img/camera-right.png");
        $(".camera-run").removeClass("camera-run");
    }
    var name1 =$("#"+cameraIndexCode).attr("name");

    if(name1=="camera-left"){
        $("#"+cameraIndexCode).attr("src","../../statics/css/img/camera-left1.png");
        $("#"+cameraIndexCode).addClass("camera-run");
    }else {
        $("#"+cameraIndexCode).attr("src","../../statics/css/img/camera-right1.png");
        $("#"+cameraIndexCode).addClass("camera-run");
    }

    window.parent.startPreview(cameraIndexCode);
}

function startPlayback(){
    var cameraIndexCode  = $("#cName").attr("name");
    if(cameraIndexCode=="" ||cameraIndexCode==undefined){
        $("#errMessage").text("请先选择监控点！");
    }else {
        var startTimeStamp = new Date($("#beginTime").val().replace('-', '/').replace('-', '/')).getTime();
        var endTimeStamp = new Date($("#endTime").val().replace('-', '/').replace('-', '/')).getTime();
        var recordLocation = 0; // 录像存储位置 0中心存储 1设备存储
        var transMode = 1; //传输协议 1TCP 2UDP
        var gpuMode = 0; //GPU硬解 0不启用 1启用
        var wndId = -1;  //默认为选中窗口回放
        var isDirectEzviz = 0;//萤石回放不启用
        if (!cameraIndexCode) {
            alert("监控点编号不能为空！");
            return
        }
        if (!Number.isNaN) {
            Number.isNaN = function(n) {
                return (
                    typeof n === "number" &&
                    window.isNaN( n )
                );
            };
        }
        if (Number.isNaN(+startTimeStamp) || Number.isNaN(+endTimeStamp)) {
            alert("时间格式有误！");
            return
        }

        window.parent.oWebControl.JS_RequestInterface({
            funcName: "startPlayback",
            argument: JSON.stringify({
                cameraIndexCode: cameraIndexCode,
                startTimeStamp: Math.floor(startTimeStamp / 1000).toString(),
                endTimeStamp: Math.floor(endTimeStamp / 1000).toString(),
                recordLocation: recordLocation,
                transMode: transMode,
                gpuMode: gpuMode,
                wndId: wndId,
                ezvizDirect: isDirectEzviz
            })
        }).then(function (oData) {

        });
    }
}

// 停止回放
function stopPlayback(){
    window.parent.oWebControl.JS_RequestInterface({
        funcName: "stopAllPlayback"
    }).then(function (oData) {
    });
}

 var setting = {
    /* view: {
         fontCss : {color:"black"},
     },*/
     data: {
         simpleData: {
             enable: true,
             idKey: "id",
             pIdKey: "parentId",
             rootPId: -1
         },
         key: {
             url:"nourl"
         }
     },
     /* check: {
          enable: true ,//显示复选框
          chkStyle : "radio"
      },*/
     callback:{
         onClick: click// 点击事件回调函数
     }
 };
 var ztree;
 //监控页监控列表
 function cameraTree(){
     $.get( "../../jk/jkcamara/indexsCamaraTree1",function(r){
         ztree = $.fn.zTree.init($("#indexsCamaraTree"), setting,r.indexsTree);
         var nodes=ztree.transformToArray(ztree.getNodes());
         for(var i=0;i<nodes.length;i++){
             if(nodes[i].level==0){
                 ztree.expandNode(nodes[i], true, true, true);
             }else{
                 ztree.expandNode(nodes[i], false, true, true);
             }
         }
     })
 }
 function click(event, treeId, treeNode){
     if(treeNode.isClick){
         $("#cName").text("当前监控点："+treeNode.name);
         $("#cName").attr("name",treeNode.data);
         $("#errMessage").text("");
         if(window.parent.playMode==0){
             startPreview(treeNode.data);
         }

     }
 };